package com.hamilton.erik.pt.mobile.client;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.hamilton.erik.pt.mobile.android.volley.VolleySingleton;
import com.hamilton.erik.pt.mobile.android.volley.toolbox.JsonObjectRequestWithParameters;

public class VolleyJsonRestClient {

	private static VolleyJsonRestClient instance = null;
	
	public static VolleyJsonRestClient getClient() {
		if(instance == null) {
			instance = new VolleyJsonRestClient();
		}
		
		return instance;
	}
	
	private VolleyJsonRestClient() {}
		
	public void getJsonArray(String url, 
								Response.Listener<JSONArray> onSuccess,
								Response.ErrorListener onError) {
		JsonArrayRequest getRequest =
				new JsonArrayRequest(url, onSuccess, onError);
		
		VolleySingleton.getInstance().getRequestQueue().add(getRequest);
	}
	
	public void getJsonObject(String url,
								Response.Listener<JSONObject> onSuccess,
								Response.ErrorListener onError) {
		JsonObjectRequest getRequest =
					new JsonObjectRequest(
						Request.Method.GET, url, null, onSuccess, onError);
		
		VolleySingleton.getInstance().getRequestQueue().add(getRequest);
	}
	

	
	public void postJsonObject(String url,
								Response.Listener<JSONObject> onSuccess,
								Response.ErrorListener onError,
								final Map<String, String> params) {
		JsonObjectRequestWithParameters postRequest =
					new JsonObjectRequestWithParameters(
						Request.Method.POST, url, params, onSuccess, onError);
		
		VolleySingleton.getInstance().getRequestQueue().add(postRequest);
	}
	
	public void putJsonObject(String url,
								Response.Listener<JSONObject> onSuccess,
								Response.ErrorListener onError,
								final Map<String, String> params) {
		JsonObjectRequestWithParameters putRequest =
				new JsonObjectRequestWithParameters(
						Request.Method.PUT, url, params, onSuccess, onError);
		
		VolleySingleton.getInstance().getRequestQueue().add(putRequest);
	}
	
	public void deleteJsonObject(String url,
									Response.Listener<JSONObject> onSuccess,
									Response.ErrorListener onError) {
		JsonObjectRequest getRequest =
		new JsonObjectRequest(
			Request.Method.DELETE, url, null, onSuccess, onError);
		
		VolleySingleton.getInstance().getRequestQueue().add(getRequest);
	}
}
